/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.constraints.Pattern;

/**
 *
 * @author java
 */
@Entity
@Table(name = "SALE")
@NamedQueries({
    @NamedQuery(name = "Sale.findAll", query = "SELECT s FROM Sale s"),
    @NamedQuery(name = "Sale.findById", query = "SELECT s FROM Sale s WHERE s.id = :id"),
    @NamedQuery(name = "Sale.findBySaleDate", query = "SELECT s FROM Sale s WHERE s.saleDate = :saleDate"),
    @NamedQuery(name = "Sale.findByNote", query = "SELECT s FROM Sale s WHERE s.note = :note"),
    @NamedQuery(name = "Sale.findByCompanyId", query = "SELECT s FROM Sale s WHERE s.companyId = :companyId"),
    @NamedQuery(name = "Sale.findByJvNo", query = "SELECT s FROM Sale s WHERE s.jvNo = :jvNo"),
    @NamedQuery(name = "Sale.findByType", query = "SELECT s FROM Sale s WHERE s.type = :type"),
    @NamedQuery(name = "Sale.findByPricingClass", query = "SELECT s FROM Sale s WHERE s.pricingClass = :pricingClass"),
    @NamedQuery(name = "Sale.findByIsReturn", query = "SELECT s FROM Sale s WHERE s.isReturn = :isReturn"),
    @NamedQuery(name = "Sale.findByValueDate", query = "SELECT s FROM Sale s WHERE s.valueDate = :valueDate"),
    @NamedQuery(name = "Sale.findByUserDefinedField1", query = "SELECT s FROM Sale s WHERE s.userDefinedField1 = :userDefinedField1"),
    @NamedQuery(name = "Sale.findByUserDefinedField2", query = "SELECT s FROM Sale s WHERE s.userDefinedField2 = :userDefinedField2")})

public class Sale implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    
    @Column(name = "SALE_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date saleDate;
    
    @Size(max = 50)
    @Column(name = "NOTE")
    private String note;
    
    @Column(name = "COMPANY_ID")
    private Integer companyId;
    
    @Column(name = "JV_NO")
    private Integer jvNo;
    
    @Column(name = "TYPE")
    private Integer type;
    
    @Column(name = "PRICING_CLASS")
    private Integer pricingClass;
    
    @Column(name = "IS_RETURN")
    private Boolean isReturn;
    
    @Column(name = "VALUE_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date valueDate;
    
    @Column(name = "USER_DEFINED_FIELD1")
    private Integer userDefinedField1;
    
    @Column(name = "USER_DEFINED_FIELD2")
    private Integer userDefinedField2;
    
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "cash")
    private Double cash;
    
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "visa")
    private Double visa;
    
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "cr")
    private Double credit;
    
    @Size(max = 50)
    @Column(name = "ClientName")
    private String clientName;
    
    @Size(max = 50)
    @Column(name = "MobileNumber")
    private String mobileNumber;
    
    @Pattern (regexp=".+@.+\\.[a-z]+", message="Email has invalid format")
    @Size(max = 50)
    @Column(name = "Email")
    private String email;
    
    
    
    public Double getCash() {
		return cash;
	}

	public void setCash(Double cash) {
		this.cash = cash;
	}

	public Double getVisa() {
		return visa;
	}

	public void setVisa(Double visa) {
		this.visa = visa;
	}

	public Double getCredit() {
		return credit;
	}

	public void setCredit(Double credit) {
		this.credit = credit;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@OneToMany(mappedBy = "saleId", fetch = FetchType.EAGER , cascade=CascadeType.ALL)
    private Set<SaleLine> saleLinesList;
    
    @JoinColumn(name = "LAST_MODIFY_USER", referencedColumnName = "USER_ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private User lastModifyUser;
    
    @JoinColumn(name = "CREATE_USER", referencedColumnName = "USER_ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private User createUser;

    @JoinColumn(name = "NAME_ID", referencedColumnName = "ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Name nameId;

    
    @Transient
    private double total ;	
    
    
    public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	public Sale() {
		this.setSaleDate(new Date());
		this.setValueDate(new Date());
		this.setCompanyId(7);
		this.setTotal(0);
		this.setIsReturn(false);
		this.setJvNo(this.getId());
		this.setType(1);
		this.setCash(0d);
		this.setVisa(0d);
		this.setCredit(0d);
    }

    public Sale(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getSaleDate() {
        return saleDate;
    }

    public void setSaleDate(Date saleDate) {
        this.saleDate = saleDate;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Integer getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Integer companyId) {
        this.companyId = companyId;
    }

    public Integer getJvNo() {
        return jvNo;
    }

    public void setJvNo(Integer jvNo) {
        this.jvNo = jvNo;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getPricingClass() {
        return pricingClass;
    }

    public void setPricingClass(Integer pricingClass) {
        this.pricingClass = pricingClass;
    }

    public Boolean getIsReturn() {
        return isReturn;
    }

    public void setIsReturn(Boolean isReturn) {
        this.isReturn = isReturn;
    }

    public Date getValueDate() {
        return valueDate;
    }

    public void setValueDate(Date valueDate) {
        this.valueDate = valueDate;
    }

    public Integer getUserDefinedField1() {
        return userDefinedField1;
    }

    public void setUserDefinedField1(Integer userDefinedField1) {
        this.userDefinedField1 = userDefinedField1;
    }

    public Integer getUserDefinedField2() {
        return userDefinedField2;
    }

    public void setUserDefinedField2(Integer userDefinedField2) {
        this.userDefinedField2 = userDefinedField2;
    }

    public Set<SaleLine> getSaleLinesList() {
        return saleLinesList;
    }

    public void setSaleLinesList(Set<SaleLine> saleLinesList) {
        this.saleLinesList = saleLinesList;
    }

    public User getLastModifyUser() {
        return lastModifyUser;
    }

    public void setLastModifyUser(User lastModifyUser) {
        this.lastModifyUser = lastModifyUser;
    }

    public User getCreateUser() {
        return createUser;
    }

    public void setCreateUser(User createUser) {
        this.createUser = createUser;
    }

    public Name getNameId() {
        return nameId;
    }

    public void setNameId(Name nameId) {
        this.nameId = nameId;
    }
    
    
    public Integer getBusinessPrimaryKey()
    {
        //return  (viewNo==null?"":viewNo.toString()) + ((itemId==null)?"":itemId.getId()) + (quantity==null?"":quantity.toString())  ;
    	return new Integer( (jvNo==null?"":jvNo) + "" + (companyId==null?"":companyId) + "" + (type==null?"":type)) ; 
    }
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getBusinessPrimaryKey() != null ? getBusinessPrimaryKey().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Sale)) {
            return false;
        }
        Sale other = (Sale) object;
        if ((this.getBusinessPrimaryKey() == null && other.getBusinessPrimaryKey() != null) || (this.getBusinessPrimaryKey() != null && !this.getBusinessPrimaryKey().equals(other.getBusinessPrimaryKey()))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getBusinessPrimaryKey()+"";
    }
    
}
